package com.gjs.cd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.cd.pojo.dto.CdResourceReq;
import com.gjs.cd.pojo.entity.CdResource;
import com.gjs.cd.service.ICdResourceService;
import com.gjs.common.enums.entity.StatusEnum;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.pojo.dto.request.PageQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhongwenguang
 * @date 2022-3-17 15:25
 */
@RestController
@Slf4j
@Api(tags = "cd资源管理")
@RequestMapping("/cd_resource")
@RequiredArgsConstructor
public class CdResourceController {

    private final ICdResourceService iCdResourceService;

    @ApiOperation("资源列表")
    @GetMapping("/list")
    public RspBody<IPage<CdResourceReq>> getListByCdId(PageQuery pageQuery, Long cdId){
        return RspBody.success(iCdResourceService.getListByCdId(pageQuery,cdId));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public RspBody<Boolean> add(List<CdResource> cdResourceList){
        return RspBody.success(iCdResourceService.saveBatch(cdResourceList));
    }

    @ApiOperation("上下架操作")
    @PostMapping("/put_or_off")
    // todo post请求入参且前端的content-type为application/json时需要为一个java bean对象，否则拿不到参数的
    public RspBody<Boolean> del(Long id, StatusEnum statusEnum){
        LambdaUpdateWrapper<CdResource> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CdResource::getId,id);
        lambdaUpdateWrapper.set(CdResource::getStatus,statusEnum);
        return RspBody.success(iCdResourceService.update(lambdaUpdateWrapper));
    }

}
